/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.jeeplus.modules.ap.entity.Fine;
import com.jeeplus.modules.ap.service.FineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.infor.entity.ProfitAnalysis;
import com.jeeplus.modules.infor.mapper.ProfitAnalysisMapper;

/**
 * 利润分析总表Service
 * @author 李超辉
 * @version 2018-09-26
 */
@Service
@Transactional(readOnly = true)
public class ProfitAnalysisService extends CrudService<ProfitAnalysisMapper, ProfitAnalysis> {
	@Autowired
	private FineService fineService;

	public ProfitAnalysis get(String id) {
		return super.get(id);
	}
	
	public List<ProfitAnalysis> findList(ProfitAnalysis profitAnalysis) {
		return super.findList(profitAnalysis);
	}
	
	public Page<ProfitAnalysis> findPage(Page<ProfitAnalysis> page, ProfitAnalysis profitAnalysis) {
		return super.findPage(page, profitAnalysis);
	}
	public Page<ProfitAnalysis> findPage1(Page<ProfitAnalysis> page, ProfitAnalysis profitAnalysis) {
			List<ProfitAnalysis> liat=getListByYue();
		DecimalFormat df = new DecimalFormat("0.00%");
		NumberFormat nf = NumberFormat.getInstance();
			for(ProfitAnalysis p:liat){
				double zxfk=0.0;
				double czfk=0.0;
				double ysfk=0.0;
				Fine fine=new Fine();
				fine.setDate(p.getDate());
				fine.setPro(p.getPro());
				fine.setJf(1);
				fine.setType("装卸");
				List<Fine> fines=fineService.findList(fine);
				for(Fine fi:fines){
					zxfk+=Double.parseDouble(fi.getZxje());
				}
				fine.setType("仓租");
				List<Fine> fine1=fineService.findList(fine);
				for(Fine fi:fine1){
					czfk+=Double.parseDouble(fi.getZxje());
				}
				fine.setType("现场管理");
				List<Fine> fine2=fineService.findList(fine);
				for(Fine fi:fine2){
					ysfk+=Double.parseDouble(fi.getZxje());
				}
				fine.setType("未上车");
				List<Fine> fines1=fineService.findList(fine);
				for(Fine fi:fines1){
					ysfk+=Double.parseDouble(fi.getZxje());
				}
				fine.setType("延迟送达");
				List<Fine> fines2=fineService.findList(fine);
				for(Fine fi:fines2){
					ysfk+=Double.parseDouble(fi.getZxje());
				}
				p.setCzfk(nf.format(czfk).replaceAll(",",""));
				p.setYsfk(nf.format(ysfk).replaceAll(",",""));
				p.setZxfk(nf.format(zxfk).replaceAll(",",""));
				String czsr=p.getCzsr();
				String czzc=p.getCzzc();
				String zxsr=p.getZxsr();
				String zxzc=p.getZxzc();
				if(czsr==null||czsr==""){
					czsr="0";
					p.setCzsr("0");
				}
				if(czzc==null||czzc==""){
					czzc="0";
					p.setCzzc("0");
				}
				if(zxsr==null||zxsr==""){
					zxsr="0";
					p.setZxsr("0");
				}
				if(zxzc==null||zxzc==""){
					zxzc="0";
					p.setZxzc("0");
				}
				double zsr=Double.parseDouble(p.getYssr())+Double.parseDouble(zxsr)+Double.parseDouble(czsr);
				double zzc=Double.parseDouble(p.getYszc())+Double.parseDouble(zxzc)+Double.parseDouble(czzc)+ysfk+czfk+zxfk;
               p.setZsr(nf.format(zsr).replaceAll(",",""));
               p.setZzc(nf.format(zzc).replaceAll(",",""));
               String mlr=p.getMlr();
               String sj=p.getSj();
				if(mlr==null||mlr==""){
					mlr="0";
					p.setMlr("0");
				}
				if(sj==null||sj==""){
					sj="0";
					p.setSj("0");
				}
               double mlr1=Double.parseDouble(mlr)-(ysfk+czfk+zxfk);
               double lrl=mlr1/zsr;
               p.setMlr(nf.format(mlr1).replaceAll(",",""));
               p.setLrl(df.format(lrl));

			}
			page.setList(liat);
			return page;

	}
	public Page<ProfitAnalysis> findPage2(Page<ProfitAnalysis> page, ProfitAnalysis profitAnalysis) {
		List<ProfitAnalysis> liat=getListBybsc();
		DecimalFormat df = new DecimalFormat("0.00%");
		NumberFormat nf = NumberFormat.getInstance();
		for(ProfitAnalysis p:liat){
			double zxfk=0.0;
			double czfk=0.0;
			double ysfk=0.0;
			Fine fine=new Fine();
			fine.setDate(p.getDate());
			fine.setPro(p.getPro());
			fine.setJf(1);
			fine.setType("装卸");
			List<Fine> fines=fineService.findList(fine);
			for(Fine fi:fines){
				zxfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("仓租");
			List<Fine> fine1=fineService.findList(fine);
			for(Fine fi:fine1){
				czfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("现场管理");
			List<Fine> fine2=fineService.findList(fine);
			for(Fine fi:fine2){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("未上车");
			List<Fine> fines1=fineService.findList(fine);
			for(Fine fi:fines1){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("延迟送达");
			List<Fine> fines2=fineService.findList(fine);
			for(Fine fi:fines2){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			p.setCzfk(nf.format(czfk).replaceAll(",",""));
			p.setYsfk(nf.format(ysfk).replaceAll(",",""));
			p.setZxfk(nf.format(zxfk).replaceAll(",",""));
			String czsr=p.getCzsr();
			String czzc=p.getCzzc();
			String zxsr=p.getZxsr();
			String zxzc=p.getZxzc();
			if(czsr==null||czsr==""){
				czsr="0";
				p.setCzsr("0");
			}
			if(czzc==null||czzc==""){
				czzc="0";
				p.setCzzc("0");
			}
			if(zxsr==null||zxsr==""){
				zxsr="0";
				p.setZxsr("0");
			}
			if(zxzc==null||zxzc==""){
				zxzc="0";
				p.setZxzc("0");
			}
			double zsr=Double.parseDouble(p.getYssr())+Double.parseDouble(zxsr)+Double.parseDouble(czsr);
			double zzc=Double.parseDouble(p.getYszc())+Double.parseDouble(zxzc)+Double.parseDouble(czzc)+ysfk+czfk+zxfk;
			p.setZsr(nf.format(zsr).replaceAll(",",""));
			p.setZzc(nf.format(zzc).replaceAll(",",""));
			String mlr=p.getMlr();
			String sj=p.getSj();
			if(mlr==null||mlr==""){
				mlr="0";
				p.setMlr("0");
			}
			if(sj==null||sj==""){
				sj="0";
				p.setSj("0");
			}
			double mlr1=Double.parseDouble(mlr)-(ysfk+czfk+zxfk);
			double lrl=mlr1/zsr;
			p.setMlr(nf.format(mlr1).replaceAll(",",""));
			p.setLrl(df.format(lrl));

		}
		page.setList(liat);
		return page;

	}
	public Page<ProfitAnalysis> findPage3(Page<ProfitAnalysis> page, ProfitAnalysis profitAnalysis) {
		List<ProfitAnalysis> liat=getListByyear();
		DecimalFormat df = new DecimalFormat("0.00%");
		NumberFormat nf = NumberFormat.getInstance();
		for(ProfitAnalysis p:liat){
			double zxfk=0.0;
			double czfk=0.0;
			double ysfk=0.0;
			Fine fine=new Fine();
			fine.setYear(p.getDate());
			fine.setPro(p.getPro());
			fine.setJf(1);
			fine.setType("装卸");
			List<Fine> fines=fineService.findList(fine);
			for(Fine fi:fines){
				zxfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("仓租");
			List<Fine> fine1=fineService.findList(fine);
			for(Fine fi:fine1){
				czfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("现场管理");
			List<Fine> fine2=fineService.findList(fine);
			for(Fine fi:fine2){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("未上车");
			List<Fine> fines1=fineService.findList(fine);
			for(Fine fi:fines1){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			fine.setType("延迟送达");
			List<Fine> fines2=fineService.findList(fine);
			for(Fine fi:fines2){
				ysfk+=Double.parseDouble(fi.getZxje());
			}
			p.setCzfk(nf.format(czfk).replaceAll(",",""));
			p.setYsfk(nf.format(ysfk).replaceAll(",",""));
			p.setZxfk(nf.format(zxfk).replaceAll(",",""));
			String czsr=p.getCzsr();
			String czzc=p.getCzzc();
			String zxsr=p.getZxsr();
			String zxzc=p.getZxzc();
			if(czsr==null||czsr==""){
				czsr="0";
				p.setCzsr("0");
			}
			if(czzc==null||czzc==""){
				czzc="0";
				p.setCzzc("0");
			}
			if(zxsr==null||zxsr==""){
				zxsr="0";
				p.setZxsr("0");
			}
			if(zxzc==null||zxzc==""){
				zxzc="0";
				p.setZxzc("0");
			}
			double zsr=Double.parseDouble(p.getYssr())+Double.parseDouble(zxsr)+Double.parseDouble(czsr);
			double zzc=Double.parseDouble(p.getYszc())+Double.parseDouble(zxzc)+Double.parseDouble(czzc)+ysfk+czfk+zxfk;
			p.setZsr(nf.format(zsr).replaceAll(",",""));
			p.setZzc(nf.format(zzc).replaceAll(",",""));
			String mlr=p.getMlr();
			String sj=p.getSj();
			if(mlr==null||mlr==""){
				mlr="0";
				p.setMlr("0");
			}
			if(sj==null||sj==""){
				sj="0";
				p.setSj("0");
			}
			double mlr1=Double.parseDouble(mlr)-(ysfk+czfk+zxfk);
			double lrl=mlr1/zsr;
			p.setMlr(nf.format(mlr1).replaceAll(",",""));
			p.setLrl(df.format(lrl));

		}
		page.setList(liat);
		return page;

	}
	public List<ProfitAnalysis> getListByYue(){
		return  mapper.getListByYue();
	};
	public List<ProfitAnalysis> getListBybsc(){
		return  mapper.getListBybsc();
	};
	public List<ProfitAnalysis> getListByyear(){
		return  mapper.getListByyear();
	};
	@Transactional(readOnly = false)
	public void save(ProfitAnalysis profitAnalysis) {
		super.save(profitAnalysis);
	}
	
	@Transactional(readOnly = false)
	public void delete(ProfitAnalysis profitAnalysis) {
		super.delete(profitAnalysis);
	}
	
}